package com.freud.opc.utgard.cases;

import static com.freud.opc.utgard.BaseConfiguration.CONFIG_ITEMIDS;
import static com.freud.opc.utgard.BaseConfiguration.getEntryValue;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import org.jinterop.dcom.common.JIException;
import org.openscada.opc.lib.da.Group;
import org.openscada.opc.lib.da.Item;
import org.openscada.opc.lib.da.Server;

import com.freud.opc.utgard.BaseConfiguration;

/**
 * 同步读取某个点位的值
 * 
 * @author Freud
 * 
 */
public class OPCTest3 {

	public static void main(String[] args) throws Exception {

		Server server = new Server(BaseConfiguration.getCLSIDConnectionInfomation(),
				Executors.newSingleThreadScheduledExecutor());

		server.connect();

		Group group = server.addGroup();
		Item item = group.addItem("MicroWin.120plc.hot_water_tank_level");

		Map<String, Item> items = group.addItems(getStringItemIds());

		dumpItem(item);

		for (Entry<String, Item> temp : items.entrySet()) {
			dumpItem(temp.getValue());
		}
		Thread.sleep(1000 * 1000);
		server.dispose();
	}

	private static void dumpItem(Item item) throws JIException {
		/*
		 * System.out.println("[" + (++count) + "],ItemName:[" + item.getId() +
		 * "],value:" + item.read(false).getValue());
		 */
		System.out.println("leixing------" + item.getId() + "----------" + item.read(false).getValue().getType());
	}

	private static String[] getStringItemIds() {

		String itemIds = getEntryValue(CONFIG_ITEMIDS);
		List<String> itemIds_list = Arrays.asList(itemIds.split(",")).stream().map(s -> s.trim())
				.collect(Collectors.toList());
		String[] need_ids = itemIds_list.toArray(new String[itemIds_list.size()]);
		return need_ids;
	}

	private static int count;
}
